package com.openx.xoj.controller;

import com.openx.xoj.model.entity.Title;
import com.openx.xoj.service.TitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/titles")
public class TitleController {

    private final TitleService titleService;

    @Autowired
    public TitleController(TitleService titleService) {
        this.titleService = titleService;
    }

    @GetMapping("/getAll")
    public ResponseEntity<List<Title>> getAllTitles() {
        List<Title> titles = titleService.getAllTitles();
        return ResponseEntity.ok(titles);
    }

    @GetMapping("/get/{id}")
    public ResponseEntity<Title> getTitleById(@PathVariable Long id) {
        Title title = titleService.getTitleById(id);
        return title != null ? ResponseEntity.ok(title) : ResponseEntity.notFound().build();
    }

    @PostMapping("/create")
    public ResponseEntity<Boolean> createTitle(@RequestBody Title title) {
        boolean created = titleService.createTitle(title);
        return ResponseEntity.ok(created);
    }

    @PutMapping("/update")
    public ResponseEntity<Boolean> updateTitle(@RequestBody Title title) {
        boolean updated = titleService.updateTitle(title);
        return ResponseEntity.ok(updated);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Boolean> deleteTitle(@PathVariable Long id) {
        boolean deleted = titleService.deleteTitle(id);
        return ResponseEntity.ok(deleted);
    }
}
